/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "DatabaseUtil.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>

#include "SqlServer.h"

/**
**/
DatabaseUtil::DatabaseUtil()
{
	registerArg( "host", m_host );
	registerArg( "port", m_port );
	registerArg( "db", m_db, true );
	registerArg( "user", m_user, true );
	registerArg( "passwd", m_passwd );
}

/**
**/
DatabaseUtil::~DatabaseUtil()
{
}

/**
**/
bool DatabaseUtil::run()
{
	Bio::SqlServerSettings	settings;
	Bio::SqlServer			server;

	settings.setServerType( Bio::SqlServerSettings::PostgreSQL ); //Bio::SqlServer::MySQL );

	if( !m_host.isEmpty() )
		settings.setHostName( m_host );
	else
		settings.setHostName( "127.0.0.1" ); //"localhost" );

	if( !m_port.isEmpty() )
		settings.setPort( m_port.toInt() );

	settings.setDatabaseName( m_db );
	settings.setUserName( m_user );
	if( !m_passwd.isEmpty() )
		settings.setPassword( m_passwd );

	if( !server.connect( settings ) )
		return false;

	bool	status = run( server );

	server.close();

	return status;
}
